package com.huxi.lang.validation.predicate;

import java.util.Objects;

public class Pair implements VPredicate {
	private final boolean and;
	private final VPredicate first;
	private final VPredicate last;

	Pair(VPredicate first, boolean and, VPredicate last) {
		this.first = first;
		this.last = last;
		this.and = and;
	}

	public static Pair and(VPredicate first, VPredicate last) {
		Objects.requireNonNull(first);
		Objects.requireNonNull(last);
		return new Pair(first, true, last);
	}

	public static Pair or(VPredicate first, VPredicate last) {
		Objects.requireNonNull(first);
		Objects.requireNonNull(last);
		return new Pair(first, false, last);
	}

	@Override
	public boolean test(Object t, boolean valueIfNull, boolean valueIfIllegalType) {
		if (and) {
			return first.test(t, valueIfNull, valueIfIllegalType) && last.test(t, valueIfNull, valueIfIllegalType);
		} else {
			return first.test(t, valueIfNull, valueIfIllegalType) || last.test(t, valueIfNull, valueIfIllegalType);
		}
	}

}
